<?

class graphSingleProcessorUsage {
	private $device;
	private $rrdPath =  '';
	private $bgColor = '#fff';
	public $inbitsMaxColor = '#aDEB7B';
	public $inbitsColor = '#CDEB8B';
	public $start = '-1d';
	public $height = 0;
	public $width = 0;
	public $legend = true;
	public function __construct(&$device,$params=array()) {
		$this->device = $device;
		
		if (is_array($params) || is_object($params)) {
			
			foreach ($params AS $name=>$value) {
				switch ($name) {
					case 'start':
						$this->$name = $value;
						break;
					case 'processor_id':
					case 'width':
					case 'height':
						$this->$name = (int)$value;
						break;
					case 'legend':
						$this->$name = (bool)$value;
						break;
				}
			}
		}
		$this->rrdPath = $device->getRrdPath().'processor_'.$this->processor_id.'_load.rrd';
		
	}
	
	public function generate($path) {
		$opts =array();
		$opts[] = '--start';
		$opts[] = $this->start;	
		$opts[] = '--full-size-mode';
		$opts[] = '--rigid';
		if (!$this->legend) {
			$opts[] = '--no-legend';
		}
		$opts[] = '-c';
		$opts[] = 'BACK#fff';
		$opts[] = '-c';
		$opts[] = 'SHADEA#fff';
		$opts[] = '-c';
		$opts[] = 'SHADEB#fff';
		$opts[] = '-c';
		$opts[] = 'MGRID#999';
		$opts[] = '-c';
		$opts[] = 'GRID#ccc';
		$opts[] = '-c';
		$opts[] = 'ARROW#fff';
		$opts[] = '-c';
		$opts[] = 'FONT#999';
		$opts[] = '--font';
		$opts[] = 'AXIS:6:mono';
		$opts[] = '--font';
		$opts[] = 'UNIT:6:mono';
		$opts[] = '--font';
		$opts[] = 'LEGEND:7:mono';

		if (!empty($this->height) && $this->height < 50) { $opts[] = '--only-graph';}
		if (!empty($this->height)) { $opts[] = '--height';$opts[] = $this->height; }
		if (!empty($this->width)) { $opts[] = '--width';$opts[] = $this->width; }		
		$opts[] = 'DEF:proc='.$this->rrdPath.':usage:AVERAGE';
    	$opts[] = 'LINE1:proc#ff0000:Usage';
		$opts[] = 'GPRINT:proc:LAST:%3.0lf';
    	$opts[] = 'GPRINT:proc:MAX:%3.0lf\l';
    	$opts[] = '--no-legend';
		if (rrd_graph($path,$opts,count($opts)) == 0) {
			echo rrd_error();		
		} else {

		}
	
	
	}

}